<?php 
//回调处理
include("./wxPay.php");

$obj = new wxPay();

$data = $obj->getCallbackData();

$result = '';

$return = array();

if ($data) {
	$out_trade_no = $data['out_trade_no'];
	if ($out_trade_no) {
		$pdo = new PDO('mysql:host=127.0.0.1;dbname=project','root','zqVnUy6cxsQXbpEZ');
		$pdo->exec('set names utf8');
		$select_sql = "SELECT * FROM `order` WHERE `order` = '$out_trade_no' AND `status` = 1";
		$orderData = $pdo->query($select_sql)->fetch(PDO::FETCH_ASSOC);
		if ($orderData) {
			$result = date("Y-m-d H:i:s")."@微信端认为通知未成功，重复通知";
			$return['return_code'] = 'SUCCESS'; 
			$return['return_msg'] = 'OK'; 
		}else{
			$time = time();
			$sql = "UPDATE `order` SET `status`=1,`end_time`='$time' WHERE `order` = '$out_trade_no'";
			$res = $pdo->exec($sql);
			if ($res) {
				$result = date("Y-m-d H:i:s").'@订单号：'.$out_trade_no."@支付成功,数据库状态修改成功";
				$return['return_code'] = 'SUCCESS'; 
				$return['return_msg'] = 'OK'; 
			}else{
				$result = date("Y-m-d H:i:s").'@订单号：'.$out_trade_no."@支付成功，数据库状态修改失败";
				$return['return_code'] = 'SUCCESS'; 
				$return['return_msg'] = 'OK'; 
			}
		}
	}else{
		$return['return_code'] = 'SUCCESS'; 
		$return['return_msg'] = '订单不存在'; 
		$result = date("Y-m-d H:i:s").'@订单号：不存在';
	}
}else{
	$result = date("Y-m-d H:i:s").'@回调数据为空';
	$return['return_code'] = 'SUCCESS'; 
	$return['return_msg'] = '回调数据为空'; 
}

file_put_contents("callback.log",$result.PHP_EOL,FILE_APPEND);

return $return;
 ?>
